<?php // 4.0
class quote_add {
  function loaddata() {
    global $masterclass;

    $masterclass->loadmodule('invoices');
    $masterclass->loadmodule('invoices_wap',$masterclass->mainconfig['moduledirectory'].'invoices/invoices_wap.func.php');

    $masterclass->websiteconfig['title']='Invoice - Add Items';
    
    $masterclass->site['postfields'][] = array(
      'table'=>'invoices_documents',
      'trackingid'=>'invoicesid',
      'fields'=>array(
        'documentid'=>'invoices_documentid',
        'recipientlineone'=>'invoices_recipientlineone'
      )
    );
    
    if ((isset($masterclass->sitevars['invstaticitemid']) && $masterclass->sitevars['invstaticitemid']!='') AND  (isset($masterclass->sitevars['invoicesid']) && $masterclass->sitevars['invoicesid']!='')) {
      $this->save_invoiceitem_static($masterclass->sitevars['invstaticitemid'],$masterclass->sitevars['invoicesid']);
      header("Location: ".$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory']."invoices/quote_add.php?".$masterclass->trackingvar_query());
    }
    if ((isset($masterclass->sitevars['deitemid']) && $masterclass->sitevars['deitemid']!='') AND  (isset($masterclass->sitevars['invoicesid']) && $masterclass->sitevars['invoicesid']!='')) {
      $this->invoices_removeitem($masterclass->sitevars['deitemid']);
      header("Location: ".$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory']."invoices/quote_add.php?".$masterclass->trackingvar_query());
    }
    
  return;
  }
  
  function savedata() {
    global $masterclass;

    $dbinfo = array(
     'table'=>'invoices_documents',
     'trackingid'=>'invoicesid',
     'static'=>array(
       'documenttype'=>'invoice'
      )
    );
    $masterclass->db_update($dbinfo);
    $masterclass->site['forward']=$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'invoices/invoice.php?'.$masterclass->trackingvar_query();

  return;
  }

  function save_invoiceitem_static($staticid='',$invoiceid='') {
    global $masterclass;

    $staticitem=$masterclass->allclasses['invoices']->fetch_invoices_staticitem($staticid);
    if (!empty($staticitem)) {
    $dbinfo = array(
      'table'=>'invoices_documentitems',
      'static'=>array(
        'qty'=>$staticitem[0]['qty'],
        'name'=>$staticitem[0]['name'],
        'description'=>$staticitem[0]['description'],
        'cost'=>$staticitem[0]['cost'],
        'taxed'=>$staticitem[0]['taxed'],
        'taxpercent'=>$staticitem[0]['taxpercent'],
        'personnel'=>$masterclass->site['personnelid'],
        'invoices'=>$invoiceid
      )
    );
    $masterclass->uploaddata($dbinfo);
    }
    
  return;
  }
  function invoices_removeitem($itemid) {
    global $masterclass;

    if ($itemid!='') {
      $strwhere=sprintf(" `id`=%d ",$masterclass->allclasses['mysql_functions']->formatmysqldbl($itemid));

      $dbinfo = array(
        'table'=>'invoices_documentitems',
        'strwhere'=>$strwhere
      );
      $masterclass->db_delete($dbinfo);
    }
  return;
  }

  function display() {
    global $masterclass;

    $arrdocumentdates=$masterclass->allclasses['invoices']->fetch_invoicesdocumentdates($masterclass->sitevars['invoicesid']);
    $masterclass->sitevars['invoices_documentdate']=$arrdocumentdates[0];
    $masterclass->sitevars['invoices_duedate']=$arrdocumentdates[1];
    $masterclass->sitevars['invoices_paid']=$arrdocumentdates[2];
    
    if ($masterclass->sitevars['invoices_documentdate']=='0000-00-00' || $masterclass->sitevars['invoices_documentdate']=='') { $masterclass->sitevars['invoices_documentdate']=date("Y/m/d"); } else { $masterclass->sitevars['invoices_documentdate']=date("Y/m/d",strtotime($masterclass->sitevars['invoices_documentdate'])); }
    if ($masterclass->sitevars['invoices_duedate']=='0000-00-00' || $masterclass->sitevars['invoices_duedate']=='') { $masterclass->sitevars['invoices_duedate']=''; } else { $masterclass->sitevars['invoices_duedate']=date("Y/m/d",strtotime($masterclass->sitevars['invoices_duedate'])); }
    if ($masterclass->sitevars['invoices_paid']=='0000-00-00' || $masterclass->sitevars['invoices_paid']=='') { $masterclass->sitevars['invoices_paid']=''; } else { $masterclass->sitevars['invoices_paid']=date("Y/m/d",strtotime($masterclass->sitevars['invoices_paid'])); }


    $arrinvoiceitems=$masterclass->allclasses['invoices']->fetch_invoices_items($masterclass->sitevars['invoicesid']);
    $arrinvoicetotal=$masterclass->allclasses['invoices']->calc_invoicetotal($arrinvoiceitems);
    
    echo '<a href="'.$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'">Home</a> | <a href="'.$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'invoices/">Invoices</a> | <a href="'.$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'customers/">Customers</a><hr>';
    
    echo '<b>'.$masterclass->sitevars['invoices_recipientlineone'].' #'.$masterclass->sitevars['invoices_documentid'].'</b><br>';
    echo "<b>";
    echo number_format($arrinvoicetotal['qty'],0).' - Item Count<br>';
    echo '$'.number_format($arrinvoicetotal['subtotal'],2).' - SubTotal<br>';
    echo '$'.number_format($arrinvoicetotal['tax'],2).' - Tax<br>';
    echo '$'.number_format($arrinvoicetotal['total'],2).' - Total<br>';
    echo "</b>";
    echo '<center><b>Select Item</b></center><br>';
    $staticitems=$masterclass->allclasses['invoices']->fetch_invoices_staticitems('default');
    if (!empty($staticitems)) {
      foreach ($staticitems as $item) {
        if ($item['qty']<1) { $itemqty=1; } else { $itemqty=$item['qty']; }
        $itemcost=$item['cost']*$itemqty;
        echo '<a href="quote_add.php?invstaticitemid='.$item['id'].'&'.$masterclass->trackingvar_query().'">'.$itemqty.'&nbsp;-&nbsp;$'.number_format($itemcost,2).'&nbsp;-&nbsp;'.$item['name'].'&nbsp;</a><br>';
      }
    }
    echo '<br>';
    echo '<center><input type="submit" value=" Create Invoice "></center>';
    echo '<br>';
    echo '<center><b>Invoice Items</b></center><hr>';
  
    $listdata=$masterclass->allclasses['invoices_wap']->display_invoices_itemslist($arrinvoiceitems,$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'invoices/quote_add.php?deitemid=');
    echo $listdata['load_list'];
    
    echo '<br><center><b>Invoice Totals</b></center><hr>';
    echo number_format($arrinvoicetotal['qty'],0).' - Item Count<br>';
    echo '$'.number_format($arrinvoicetotal['subtotal'],2).' - SubTotal<br>';
    echo '$'.number_format($arrinvoicetotal['tax'],2).' - Tax<br>';
    echo '$'.number_format($arrinvoicetotal['total'],2).' - Total<br>';
  return;
  }
}
?>